﻿using Blog190311.Areas.Back.Models;
using Blog190311.Areas.Back.Models.DAL;
using Blog190311.Models.DBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Blog190311.Areas.Back.Controllers
{
    [Authorize]
    public class GalleryController : Controller
    {
        // GET: Back/Gallery
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 显示数据
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Title"></param>
        /// <param name="ShowType"></param>
        /// <param name="IsPub"></param>
        /// <param name="Tage"></param>
        /// <returns></returns>
        public ActionResult GetBypage(int limit, int page, string Title, string ShowType, string IsPub, string Tage)
        {
            GalleryService art = new GalleryService();
            List<Article> lists = art.GetBypage(limit, page, Title, ShowType, IsPub, Tage);
            int num = art.GetCount(Title, ShowType, IsPub, Tage);
            return Json(new { code = 0, msg = "", count = num, data = lists }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();
        }

        public ActionResult BaoCun(Article article)
        {
            GalleryService art = new GalleryService();
            int num = art.BaoCun(article);
            return Content(num.ToString());
        }

        /// <summary>
        ///修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            GalleryService art = new GalleryService();
            Article article = art.GetId(id);
            return View(article);
        }

        public ActionResult XiuGai(Article article)
        {
            GalleryService art = new GalleryService();
            int num = art.XiuGai(article);
            return Content(num.ToString());
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShanChu(int id)
        {
            GalleryService art = new GalleryService();
            int num = art.ShanChu(id);
            return Content(num.ToString());
        }

        public ActionResult Guanli()
        {
            return View();
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult FaBu(int id)
        {
            GalleryService art = new GalleryService();
            int num = art.FaBu(id);
            return Content(num.ToString());
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadPhoto()
        {
            try
            {
                //上传的单独文件的访问
                HttpPostedFileBase postFile = Request.Files["file1"];
                if (postFile != null)
                {
                    //文件扩展名，不含“.”
                    string fileExt = postFile.FileName.Substring(postFile.FileName.LastIndexOf(".") + 1);
                    //随机生成新的文件名
                    string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "." + fileExt;
                    //上传目录相对路径~表示网站根目录
                    string upLoadPath = "~/photos/";
                    //上传目录的物理路径
                    string fullUpLoadPath = Server.MapPath(upLoadPath);
                    //核心方法，上传文件并保存
                    postFile.SaveAs(fullUpLoadPath + newFileName);
                    return Json(new UIPicture { Success = true, Message = "", Remark = "photos/" + newFileName });
                }
                else
                {
                    return Json(new UIPicture { Success = false, Message = "未检测到文件数据" });
                }
            }
            catch (Exception ex)
            {

                return Json(new UIPicture { Success = false, Message = "上传失败:" + ex.Message });
            }
        }
    }
}